//
//  BDFillBusinessInfoStep4ViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/13.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "BDFillBusinessInfoStep4ViewController.h"
#import "UIViewController+BDAddBusinessHelper.h"
#import "UIImagePickerController+Block.h"
#import "UIImage+Tools.h"
#import "BQActivityView.h"
#import "BDAddBusinessStep4Request.h"
#import "NSString+Validation.h"


@interface BDFillBusinessInfoStep4ViewController ()

@property (weak, nonatomic) IBOutlet UITextField *corporateNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *legalPersonContactPhone;

@property (weak, nonatomic) IBOutlet UIImageView *legalPersonIDCardFrontalPic;
@property (weak, nonatomic) IBOutlet UIImageView *legalPersonIDCardbackPic;
@property (weak, nonatomic) IBOutlet UIImageView *contractSigningPic;

@property (weak, nonatomic) IBOutlet UIButton *legalPersonIDCardFrontalPicBtn;
@property (weak, nonatomic) IBOutlet UIButton *legalPersonIDCardbackPicBtn;
@property (weak, nonatomic) IBOutlet UIButton *contractSigningPicBtn;


@end

@implementation BDFillBusinessInfoStep4ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.corporateNameTextField.delegate = self;
    self.legalPersonContactPhone.delegate = self;
    
    [self.titleButton setTitle:@"店铺资料" forState:UIControlStateNormal];
    [self.rightAreaRBtn setTitle:@"下一步" forState:UIControlStateNormal];
    self.rightAreaRBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [self.rightAreaRBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.rightArea.mas_right);
        make.width.mas_equalTo(@50);
        make.centerY.mas_equalTo(self.rightArea.mas_centerY);
        make.height.mas_equalTo(@30);
    }];
    
    [self fillbackIfNeed];
}

- (void)fillbackIfNeed{
    if (!self.userShopModel) return;
    self.corporateNameTextField.text = self.userShopModel.appBizShop.contactName;
    self.legalPersonContactPhone.text = self.userShopModel.appBizShop.contactCellphone;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)handleNavigationAction:(HSYNavigationActionType)actionType{
    if (actionType == HSYNavActionLeftAreaLeft) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    if (actionType == HSYNavActionRightAreaRight) {
        [self goNext:nil];
    }
}


- (void)commitData{
    [self.view endEditing:YES];
    
    if (![self checkDataIntegrity]) return;
    
    BDAddBusinessStep4Request *task = [[BDAddBusinessStep4Request alloc] init];
    task.shopId = [NSString stringWithFormat:@"%ld", (long)self.model.shopId];
    task.uid = [NSString stringWithFormat:@"%ld", (long)self.model.uid];
    task.contactName = self.corporateNameTextField.text;
    task.contactCellphone = self.legalPersonContactPhone.text;
    
    task.fileA = UIImageJPEGRepresentation(self.legalPersonIDCardFrontalPic.image, 0.1);
    task.fileB = UIImageJPEGRepresentation(self.legalPersonIDCardbackPic.image, 0.1);
    task.fileC = UIImageJPEGRepresentation(self.contractSigningPic.image, 0.1);
    
    
    [BQActivityView showActiviTy];
    __weak typeof(self)weakSelf = self;
    
    [BDNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        [BQActivityView hideActiviTy];
        
        if (responseObject) {
            BDBusinessModel *model = [BDBusinessModel mj_objectWithKeyValues:responseObject];
            weakSelf.model.status = model.status;
            weakSelf.model.auStep = model.auStep;
            //go to next step
            [weakSelf nextStepWihBusinessModel:weakSelf.model userShopModel:weakSelf.userShopModel readOnly:weakSelf.readOnly];
            
        }else{
            if (resultString) {
                [weakSelf showMessage:resultString];
            }
        }
    }];

}

- (IBAction)goNext:(id)sender {
    [self handleNext];
}



-(BOOL)checkDataIntegrity{
    
    if (!self.corporateNameTextField.text.length) {
        [self showMessage:@"请填写联系人姓名"];
        return NO;
    }else if (![self.corporateNameTextField.text isValidPersonName]){
        [self showMessage:@"请输入正确联系人名称"];
        return NO;
    }
    
    if (!self.legalPersonContactPhone.text.length) {
        [self showMessage:@"请填写联系电话"];
        return NO;
    }else if (![self.legalPersonContactPhone.text isValidPhone]){
        [self showMessage:@"手机号错误，请重新输入"];
        return NO;
    }
    
    
    if (!self.legalPersonIDCardFrontalPic.image) {
        [self showMessage:@"请拍摄法人正面照"];
        return NO;
    }
    
    if (!self.legalPersonIDCardbackPic.image) {
        [self showMessage:@"请拍摄法人背面照"];
        return NO;
    }
    
    if (!self.contractSigningPic.image) {
        [self showMessage:@"请拍签约合同照"];
        return NO;
    }
    
    return YES;
}



- (IBAction)legalPersonIDCardFrontalPicBtnClick:(UIButton *)sender {
    if (self.readOnly) return;
    __weak typeof(self) wSelf = self;
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] initWithSourceType:UIImagePickerControllerSourceTypeCamera finish:^(UIImagePickerController *vc, UIImage *image) {
        [wSelf.legalPersonIDCardFrontalPicBtn setImage:nil forState:UIControlStateNormal];
        wSelf.legalPersonIDCardFrontalPic.image = image;
    }];
    
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}

- (IBAction)legalPersonIDCardbackPicBtnClick:(UIButton *)sender {
    if (self.readOnly) return;

    __weak typeof(self) wSelf = self;
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] initWithSourceType:UIImagePickerControllerSourceTypeCamera finish:^(UIImagePickerController *vc, UIImage *image) {
        [wSelf.legalPersonIDCardbackPicBtn setImage:nil forState:UIControlStateNormal];
        wSelf.legalPersonIDCardbackPic.image = image;
    }];
    
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}


- (IBAction)contractSigningPicBtnClick:(id)sender {
    if (self.readOnly) return;

    __weak typeof(self) wSelf = self;
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] initWithSourceType:UIImagePickerControllerSourceTypeCamera finish:^(UIImagePickerController *vc, UIImage *image) {
        [wSelf.contractSigningPicBtn setImage:nil forState:UIControlStateNormal];
        wSelf.contractSigningPic.image = image;
    }];
    
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
